package com.it.stone_manage.service.Impl;

import com.it.stone_manage.dao.StoneAccountDao;
import com.it.stone_manage.model.StoneAccount;
import com.it.stone_manage.service.AccountService;
import com.it.stone_manage.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;

@Service
@Transactional
@Slf4j
public class AccountServiceImpl implements AccountService {
    @Autowired
    StoneAccountDao stoneAccountDao;
    private Result result = new Result();

    @Override
    public Result register(StoneAccount stoneAccount) {
        try {
            StoneAccount accountByCondition = stoneAccountDao.findAccountByCondition(stoneAccount);
            if (accountByCondition != null && accountByCondition.getUserName().equals(stoneAccount.getUserName())) {
                result.setStatus(false);
                result.setMessage("用户名已存在，请重新输入！");
                return result;
            }
            Integer addNum = stoneAccountDao.addAccount(stoneAccount);
            if (addNum != null && addNum != 0) {
                result.setStatus(true);
                result.setMessage("注册成功！");
            } else {
                result.setStatus(false);
                result.setMessage("注册失败，请稍后再试");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(false);
            result.setMessage("注册失败，请稍后再试");
        }

        return result;
    }

    @Override
    public Result login(StoneAccount stoneAccount, HttpSession session) {
        try {
//            检查session
            if (stoneAccount == null || stoneAccount.getUserName() == null || stoneAccount.getPassword() == null) {
                if (session.getAttribute("loginUser") != null) {
                    result.setStatus(true);
                    result.setMessage("登录成功！");
                } else {
                    result.setStatus(false);
                    result.setMessage("登录失败，请稍后再试");
                }
            } else {
                StoneAccount DBAccount = stoneAccountDao.findAccountByCondition(stoneAccount);
                if (DBAccount != null && DBAccount.getPassword().equals(stoneAccount.getPassword())) {
                    if (session.getAttribute("loginUser") == null) {
                        session.setAttribute("loginUser", stoneAccount);
                        result.setStatus(true);
                        result.setMessage("登录成功！");
                    } else {
                        result.setStatus(false);
                        result.setMessage("登录失败，请稍后再试");
                    }

                } else {
                    result.setStatus(false);
                    result.setMessage("用户名或密码错误！");
                }
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(false);
            result.setMessage("系统错误，请稍后再试");
        }

        return result;
    }

    @Override
    public Result logout(HttpSession session) {
        try {
            session.removeAttribute("loginUser");
            result.setStatus(true);
            result.setMessage("成功退出！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(false);
            result.setMessage("系统错误，请稍后再试或联系管理员！");
        }

        return result;
    }
}